/*
    19.Write a program in C to Concatenate Two Strings Manually.

    Test Data :
    Input the first string : this is string one
    Input the second string : this is string two

    Expected Output :

    After concatenation the string is : 
    this is string one this is string two
*/

#include <stdio.h>
#include <stdlib.h>

#define MAX_LEN 1024

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p++ = ch;
    }
    *p = '\0';
    return str;
}

int getstrlen(char *str)
{
    int len = 0;
    char *p = str;
    while(*p != '\0'){
        len++;
        p++;
    }
    len++; // '\0'
    return len;
}

char *conectstr(char *str1, char *str2)
{
    int len = getstrlen(str1) + getstrlen(str2);
    char *str = malloc(len - 1);
    char *p = str;
    while(*str++ = *str1++);
    *(--str) = ' ';
    str++;
    while(*str++ = *str2++);
    *str = '\0';
    return p;
}

int main(void)
{
    char *str1 = malloc(MAX_LEN);
    char *str2 = malloc(MAX_LEN);
    printf("Input first string : ");
    getstr(str1);
    printf("Input second string : ");
    getstr(str2);
    printf("After concatenation the string is : \n%s", conectstr(str1, str2));

    return 0;
}